package com.imooc.o2o.web.shop;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.imooc.o2o.dto.ShopExecution;
import com.imooc.o2o.entity.Area;
import com.imooc.o2o.entity.PersonInfo;
import com.imooc.o2o.entity.Shop;
import com.imooc.o2o.entity.ShopCategory;
import com.imooc.o2o.enums.ShopStateEnum;
import com.imooc.o2o.service.AreaService;
import com.imooc.o2o.service.ShopCategoryService;
import com.imooc.o2o.service.ShopService;
import com.imooc.o2o.service.impl.ShopServiceImpl;
import com.imooc.o2o.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/shopadmin")
public class ShopManagementController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private ShopCategoryService shopCategoryService;
    @Autowired
    private AreaService areaService;

    //获取店铺信息
    @RequestMapping(value="/shopinfo",method = RequestMethod.GET)
    @ResponseBody
    private Map<String,Object> getShopInitInfo(){
        Map<String,Object> modeeMap = new HashMap<String,Object>();
        List<ShopCategory> shopCategoryList= new ArrayList<ShopCategory>();
        List<Area> areaList = new ArrayList<Area>();
        try{
            shopCategoryList = shopCategoryService.getShopCategoryList(new ShopCategory());
            areaList = areaService.getAreaList();
            modeeMap.put("areaList",areaList);
            modeeMap.put("shopCategoryList",shopCategoryList);
            modeeMap.put("success",true);
        }catch(Exception e){
            modeeMap.put("massage",e.getMessage());
            modeeMap.put("success",false);
        }
        return modeeMap;
    }

    //添加新的店铺
    //HttpServletRequest包含了客户端http请求的内容
    @RequestMapping(value="/registershop",method = RequestMethod.POST)
    @ResponseBody
    private Map<String,Object> registerShop(HttpServletRequest request){
        //接受参数
        Map<String,Object> modeMap = new HashMap<String, Object>();
        String shopStr = HttpServletRequestUtil.getString(request,"shopStr");
        Shop shop = null;
        ObjectMapper mapper =new ObjectMapper();
        CommonsMultipartFile shopImg = null;
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if(commonsMultipartResolver.isMultipart(request)){
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            shopImg = (CommonsMultipartFile)multipartRequest.getFile("shopImg");
        }else{
            modeMap.put("success",false);
            modeMap.put("errMsg","上传图片不得为空");
            return modeMap;
        }
        try{
            //ObjectMapper.readValue(JSON，对象)转化为指定的对象
            //https://app.yinxiang.com/shard/s2/nl/1242232/d20edb49-1c4e-4459-b865-e50ad69dbc12
            shop = mapper.readValue(shopStr,Shop.class);
        }catch(Exception e){
            modeMap.put("success",false);
            modeMap.put("errMsg",e.getMessage());
            return modeMap;
        }
        //注册店铺
        if(shop != null && shopImg!=null){
            PersonInfo owner= new PersonInfo();
            owner.setUserId(1L);
            try{
                ShopExecution se = shopService.addShop(shop,shopImg.getInputStream(),shopImg.getOriginalFilename());
                if(se.getState() == ShopStateEnum.CHECK.getState()){
                    modeMap.put("success",true);
                }else{
                    modeMap.put("success",false);
                    modeMap.put("errMsg",se.getStateInfo());
                }
            }catch(IOException e) {
                modeMap.put("success", false);
                modeMap.put("errMsg", e.getMessage());
            }
            return modeMap;
        }else{
            modeMap.put("success",false);
            modeMap.put("errMsg","");
            return modeMap;
        }
    }

    //根据shop_id获取店铺详情
    @RequestMapping(value="/getshopbyid",method=RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getShopSyId(@RequestParam Long shopId, HttpServletRequest request){
        Map<String,Object> modeMap = new HashMap<String, Object>();
        Shop shop=shopService.queryByShopId(shopId);
        modeMap.put("shop",shop);
        return modeMap;
    }
}
